package com.kxmall.web.controller.group;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.group.domain.bo.KxGroupShopBo;
import com.kxmall.group.domain.vo.KxGroupShopVo;
import com.kxmall.web.controller.group.service.IKxGroupShopService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 团购
 *
 * @author kxmall
 * @date 2023-10-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/group/groupShop")
public class KxGroupShopController extends BaseController {

    private final IKxGroupShopService iKxGroupShopService;

    /**
     * 查询团购列表
     */
    @SaCheckPermission("group:groupShop:list")
    @GetMapping("/list")
    public TableDataInfo<KxGroupShopVo> list(KxGroupShopBo bo, PageQuery pageQuery) {
        bo.setStorageIds(getLoginUser().getStoragePermission());
        return iKxGroupShopService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出团购列表
     */
    @SaCheckPermission("group:groupShop:export")
    @Log(title = "团购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KxGroupShopBo bo, HttpServletResponse response) {
        List<KxGroupShopVo> list = iKxGroupShopService.queryList(bo);
        ExcelUtil.exportExcel(list, "团购", KxGroupShopVo.class, response);
    }

    /**
     * 获取团购详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("group:groupShop:query")
    @GetMapping("/{id}")
    public R<KxGroupShopVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(iKxGroupShopService.queryById(id));
    }

    /**
     * 新增团购
     */
    @SaCheckPermission("group:groupShop:add")
    @Log(title = "团购", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KxGroupShopBo bo) {
        return toAjax(iKxGroupShopService.insertByBo(bo));
    }

    /**
     * 修改团购
     */
    @SaCheckPermission("group:groupShop:edit")
    @Log(title = "团购", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KxGroupShopBo bo) {
        return toAjax(iKxGroupShopService.updateByBo(bo));
    }

    /**
     * 删除团购
     *
     * @param ids 主键串
     */
    @SaCheckPermission("group:groupShop:remove")
    @Log(title = "团购", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iKxGroupShopService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
